package guis.visitant;

import exceptions.RepositoryException;
import guis.RegisterPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import utils.Date;
import classes.Classroom;
import classes.Iterator;
import classes.Visitant;

import communication.Facade;

public class FindVisitantWindow extends VisitantWindow
{
	private static final long	serialVersionUID	= 1032307379765488755L;
	
	public FindVisitantWindow( JFrame source, RegisterPanel< Visitant > registerPanel, Facade facade )
	{
		super( "Buscar Classe", "buscar", source, registerPanel, facade );
		createActions();
	}
	
	private void createActions()
	{
		actionButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				String name = nameTextField.getText();
				Integer day = ( Integer ) dayComboBox.getSelectedItem();
				Integer month = ( Integer ) monthComboBox.getSelectedItem();
				Integer year = ( Integer ) yearComboBox.getSelectedItem();
				Classroom classroom = ( Classroom ) classroomComboBox.getSelectedItem();
				int classroomId = 0;
				if ( classroom != null )
					classroomId = classroom.getId();
				
				Date date = null;
				if( day == null ) day = 0;
				if( month == null ) month = 0;
				if( year == null ) year = 0;
				date = new Date( day, month, year );

				Visitant visitant = new Visitant( name, date, classroomId );

				try
				{
					Iterator< Visitant > iterator = facade.findVisitant( visitant );
					registerPanel.loadTable( iterator );
					setVisible( false );
				}
				catch ( RepositoryException e )
				{

				}
			}

		} );
		
		cancelButton.addActionListener( new ActionListener(){

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				setVisible( false );
			}
			
		});
	}
}
